package com.sk.weichat.alipay;

import android.app.Activity;
import android.arch.lifecycle.MutableLiveData;
import android.util.Log;

import com.alipay.sdk.app.PayTask;

import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * Created by lzs on 2018/11/19 19:03
 * E-Mail Address：343067508@qq.com
 */
public final class AliPayHelper {

    private static final Executor threadPool = Executors.newFixedThreadPool(1);

    public final static void pay(final String orderInfo, final Activity activity, final MutableLiveData<PayResult> liveData) {
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                PayTask alipay = new PayTask(activity);
                Map<String, String> result = alipay.payV2(orderInfo, true);
                handleResult(result,liveData);
            }
        });
    }

    private final static void handleResult(Map<String, String> result, MutableLiveData<PayResult> liveData){
        PayResult payResult = new PayResult(result);
        liveData.postValue(payResult);
        /**
         对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
         */
//        String resultInfo = payResult.getResult();// 同步返回需要验证的信息
//        String resultStatus = payResult.getResultStatus();
        // 判断resultStatus 为9000则代表支付成功

    }

    /**
     * 非主线程
     */
    public interface AlipayListener {

        void onAlipaySuccess();

        void onAlipayFailed(String msg);
    }
}
